package cn.cosmosx.base.security.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import cn.cosmosx.base.constant.ResultCode;
import cn.cosmosx.base.util.ResponseUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义无权限访问异常处理器
 */
@Slf4j
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        log.error("{}#无权限访问异常:{}", this.getClass().getName(), accessDeniedException.getMessage());
        ResponseUtils.wrapResponse(response, ResultCode.U00004, accessDeniedException.getMessage());
    }
}
